{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install statsmodels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tensorboard.backend.event_processing.event_accumulator import EventAccumulator\n",
    "import glob\n",
    "import os\n",
    "import fnmatch\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy import interpolate\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib as mplt\n",
    "import scipy.stats as st\n",
    "import statsmodels.stats.api as sms\n",
    "import colorsys\n",
    "from matplotlib.ticker import (MultipleLocator, FormatStrFormatter)\n",
    "import re\n",
    "import pathlib\n",
    "\n",
    "output_folder = 'fig'\n",
    "\n",
    "\n",
    "# MINIIMAGENET, scaling crossvalidation \n",
    "# This is with polynomial power learning\n",
    "# ROOT_DIR='/mnt/home/boris/experiments_task_encoder/180420_191842_mini_imagenet_metric_multiplier_trainable_encoder_classifier_link_feat_extract_pretrain_polynomial_metric_order_metric_multiplier_init_cbn_per_block_cbn_num_layers_repeat_more_cbn_layers'\n",
    "# experiment_patterns = [\n",
    "# #     '*cbn_num_layers=3*polynomial_metric_order=3;feat_extract_pretrain=multitask;encoder_classifier_link=cbn;cbn_per_block=False*',\n",
    "# #     '*cbn_num_layers=4*polynomial_metric_order=3;feat_extract_pretrain=multitask;encoder_classifier_link=cbn;cbn_per_block=False*',\n",
    "#     '*cbn_num_layers=5*polynomial_metric_order=3;feat_extract_pretrain=multitask;encoder_classifier_link=cbn;cbn_per_block=False*',\n",
    "# ]\n",
    "# This is laets and greatest\n",
    "ROOT_DIR='/mnt/home/boris/experiments_task_encoder/180508_195508_mini_imagenet_num_max_pools_encoder_classifier_link_feat_extract_pretrain_metric_multiplier_init_cbn_num_layers_repeat_scale_maxpool3'\n",
    "experiment_patterns = [\n",
    "    '*cbn_num_layers=3*pretrain=multitask*encoder_classifier_link=cbn*multiplier_init=7.5*',\n",
    "]\n",
    "# This is one beofre latest and greatest\n",
    "# ROOT_DIR='/mnt/home/boris/experiments_task_encoder/180422_013228_mini_imagenet_metric_multiplier_trainable_encoder_classifier_link_feat_extract_pretrain_polynomial_metric_order_metric_multiplier_init_cbn_per_block_cbn_num_layers_repeat_scaling_crossvalidation_min'\n",
    "# experiment_patterns = [\n",
    "#     '*cbn_num_layers=3*pretrain=multitask*encoder_classifier_link=cbn*multiplier_init=5*',\n",
    "# ]\n",
    "\n",
    "\n",
    "# # CIFAR100\n",
    "# ROOT_DIR='/mnt/home/boris/experiments_task_encoder/180507_015250_mini_imagenet_num_tasks_per_batch_encoder_classifier_link_feat_extract_pretrain_metric_multiplier_init_init_learning_rate_number_of_steps_cbn_num_layers_repeat_scale_crossval_CIFAR100_10shot'\n",
    "# experiment_patterns = [\n",
    "#     '*feat_extract_pretrain=multitask;encoder_classifier_link=cbn;number_of_steps=10000*metric_multiplier_init=5',\n",
    "# ]\n",
    "\n",
    "\n",
    "fig_name = \"beta_gamma_MIN.pdf\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf; print(tf.__version__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get directories for the  experiements "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_experiment_folder_list(experiement_dir, experiment_pattern):\n",
    "    list_experiemnts_pattern = []\n",
    "    list_experiments_all = os.listdir(experiement_dir)\n",
    "    for folder in list_experiments_all:\n",
    "        names = os.path.join(ROOT_DIR,folder)\n",
    "#         print(names)\n",
    "        names_filt_alone = fnmatch.filter([names], experiment_pattern)\n",
    "#         print(names_filt_alone)\n",
    "        list_experiemnts_pattern.extend(names_filt_alone)\n",
    "    return list_experiemnts_pattern\n",
    "\n",
    "\n",
    "def interpolate_data(experiment_data, index_max, step):\n",
    "    index_max = int(round(index_max/step)*step)\n",
    "    index_new = np.arange(0, index_max+step, step)\n",
    "    experiment_data_interpolated = {}\n",
    "    for file, df in experiment_data.items():\n",
    "        interpolator = interpolate.interp1d(df.index, df.value, bounds_error=False, fill_value=np.nan)\n",
    "        value_new = interpolator(index_new)\n",
    "        experiment_data_interpolated[file] = value_new\n",
    "    \n",
    "    return pd.DataFrame(index=index_new, data=experiment_data_interpolated)\n",
    "    \n",
    "\n",
    "def read_experiment_data(experiement_dir, experiment_pattern, variable, step):\n",
    "    experiemnt_folder_list = get_experiment_folder_list(experiement_dir, experiment_pattern)\n",
    "    experiment_data_tst = {}\n",
    "    experiment_data_val = {}\n",
    "    index_max_tst = 0\n",
    "    index_max_val = 0\n",
    "    \n",
    "    r_gamma = re.compile(\".*\"+variable+\".*\")\n",
    "    \n",
    "    i=0    \n",
    "    for experiement in experiemnt_folder_list:\n",
    "        print(experiement)\n",
    "        print(glob.glob1(experiement+'/train/', 'events.out.tfevents.*'))\n",
    "        df_gamma=[]\n",
    "        df_beta=[]\n",
    "        for file in glob.glob1(experiement+'/train/', 'events.out.tfevents.*'):\n",
    "            ea = EventAccumulator(experiement+'/train/'+file)\n",
    "            ea.Reload()\n",
    "            tags = ea.Tags()['scalars']\n",
    "#             print(list(filter(r_gamma.match, tags)))\n",
    "            if len(list(filter(r_gamma.match, tags))) == 0:\n",
    "                continue\n",
    "            \n",
    "            gamma_list = list(filter(r_gamma.match, tags))\n",
    "            data = ea.Scalars(gamma_list[0])\n",
    "            if len(data) > 0:\n",
    "                df_gamma.append(pd.DataFrame(data).set_index('step').drop('wall_time', axis=1))\n",
    "        \n",
    "        if len(df_gamma) > 0:\n",
    "            df_gamma = pd.concat(df_gamma)\n",
    "        \n",
    "        if len(df_gamma) > 0:\n",
    "            experiment_data_tst[experiement+file] = df_gamma\n",
    "            index_max_tst = max(index_max_tst, df_gamma.index.max())\n",
    "        \n",
    "    return interpolate_data(experiment_data_tst, index_max_tst, step=step)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_experiemnts_target_transfer = get_experiment_folder_list(ROOT_DIR, experiment_patterns[0])\n",
    "list_experiemnts_target_transfer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_data = {}\n",
    "for experiment_pattern in experiment_patterns:\n",
    "    print('Loading experiment: %s' %experiment_pattern)\n",
    "    \n",
    "    gamma_weight = read_experiment_data(ROOT_DIR, experiment_pattern, \n",
    "                                        variable='gamma_weight00', step=500)\n",
    "    \n",
    "    gamma_weight_layers = pd.DataFrame(index=np.arange(12), columns=gamma_weight.columns)\n",
    "    beta_weight_layers = pd.DataFrame(index=np.arange(12), columns=gamma_weight.columns)\n",
    "    for i in range(4):\n",
    "        for j in range(3):\n",
    "            gamma_weight = read_experiment_data(ROOT_DIR, experiment_pattern, \n",
    "                                                variable='gamma_weight'+str(i)+str(j), step=500)\n",
    "            gamma_weight_layers.iloc[3*i+j]=gamma_weight.iloc[-2]\n",
    "            \n",
    "            beta_weight = read_experiment_data(ROOT_DIR, experiment_pattern, \n",
    "                                               variable='beta_weight'+str(i)+str(j), step=500)\n",
    "            beta_weight_layers.iloc[3*i+j]=beta_weight.iloc[-2]\n",
    "    \n",
    "    plot_data[(experiment_pattern, 'gamma_weight')] = gamma_weight_layers\n",
    "    plot_data[(experiment_pattern, 'beta_weight')] = beta_weight_layers\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pathlib.Path(output_folder).mkdir(parents=True, exist_ok=True)\n",
    "\n",
    "mplt.rcParams.update({'font.size': 22})\n",
    "\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "ax1 = fig.add_subplot(2,1,1)\n",
    "ax2 = fig.add_subplot(2,1,2)\n",
    "plt.grid('on')\n",
    "\n",
    "ax1.boxplot(abs(plot_data[(experiment_patterns[0], 'gamma_weight')].T.values))\n",
    "ax1.grid(which='major', alpha=0.5) \n",
    "# ax1.set_xlabel('Conv layer #')\n",
    "ax1.set_ylabel(r'$\\gamma_0$')\n",
    "\n",
    "ax2.boxplot(abs(plot_data[(experiment_patterns[0], 'beta_weight')].T.values))\n",
    "ax2.grid(which='major', alpha=0.5) \n",
    "ax2.set_ylabel(r'$\\beta_0$')\n",
    "ax2.set_xlabel('Conv layer #')\n",
    "\n",
    "fig.savefig(os.path.join(output_folder,fig_name), bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_plot(data, edge_color, fill_color, ax):\n",
    "    bp = ax.boxplot(data, patch_artist=True)\n",
    "\n",
    "    for element in ['boxes', 'whiskers', 'fliers', 'means', 'medians', 'caps']:\n",
    "        plt.setp(bp[element], color=edge_color)\n",
    "\n",
    "    for patch in bp['boxes']:\n",
    "        patch.set(facecolor=fill_color) \n",
    "    return bp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mplt.rcParams.update({'font.size': 22})\n",
    "\n",
    "fig = plt.figure(figsize=(10,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "ax.set_yscale('log')\n",
    "plt.grid('on')\n",
    "\n",
    "bp_gamma = draw_plot(abs(plot_data[(experiment_patterns[0], 'gamma_weight')].T.values), 'red', 'tan', ax)\n",
    "bp_beta = draw_plot(abs(plot_data[(experiment_patterns[0], 'beta_weight')].T.values), 'blue', 'cyan', ax)\n",
    "ax.set_xlabel('Conv layer #')\n",
    "ax.set_ylim([3e-6, 0.6])\n",
    "\n",
    "ax.legend([bp_gamma[\"boxes\"][0], bp_beta[\"boxes\"][0]], [r'$\\gamma_0$', r'$\\beta_0$'], loc='upper left')\n",
    "\n",
    "fig.savefig(os.path.join(output_folder,fig_name), bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
